<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>查找质数</title>
</head>
<body>
    <script>
        //简单判断是否为质数
            //质数:只能被自己和1整除的自然数
        /* function isPrime(num) {  
            for (let i = 2; i < num; i++) {
                if(num % i == 0){
                    return false;
                }
            }
            return true;
        } */
        //效率高些的方法
            //质数的因子小于等于这个数的开平方根
        function isPrime(num){
            let sqrtNum = parseInt(Math.sqrt(num));
            for (let i = 2; i <= sqrtNum; i++) {
                if(num % i == 0){
                    return false;
                }
            }
            return true;
        }

        console.log(isPrime(2));
        console.log(isPrime(4));
        console.log(isPrime(7));
        console.log(isPrime(37));
        console.log(isPrime(36));
    </script>
</body>
</html>